﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using ST.Buz.Base.Entity;
using ST.Buz.Base.Enums;
using ST.Buz.Base.ViewModel;
using ST.Buz.Service;
using ST.Common.Util;
using ST.Common.Util.Model;
using ST.Common.Util.Mvc;
using ST.Common.Util.Text;
using System;
using System.Net;

namespace ST.Buz.Web.Controllers
{
	[Route("sys/user")]
	public class Sys_UserController : MyBaseController
	{
		private readonly IServiceSysUser service;
		private readonly IServiceSysOrganization serviceSysOrganization;
		private readonly IServiceSysAttachment serviceSysAttachment;
		private readonly IServiceSysRole serviceSysRole;
		private VM_RelatedModule[] relatedModules;

		public Sys_UserController(
			IServiceSysUser _service,
			IServiceSysOrganization _serviceSysOrganization,
			IServiceSysAttachment _serviceSysAttachment,
			IServiceSysRole _serviceSysRole,
			ILogger<Sys_UserController> _logger) : base(_logger)
		{
			service = _service;
			serviceSysOrganization = _serviceSysOrganization;
			serviceSysAttachment = _serviceSysAttachment;
			serviceSysRole = _serviceSysRole;
			relatedModules = new VM_RelatedModule[]{
				new VM_RelatedModule(){
					id="sys_attachment",
					name="相关附件",
					count=0
				}
			};
		}

		public IActionResult Index()
		{
			return RedirectToAction("List");
		}

		[ValidateSession]
		[HttpGet("list")]
		public IActionResult List()
		{
			try
			{
				UserSession sessionCurrent = GetSessionFromRequest();
				ViewBag.ServiceCode = "1DE4B6114D";
				ViewBag.StateOptions = GetEnums(typeof(UserState));
				ViewBag.GenderOptions = GetEnums(typeof(Gender));
				ViewBag.OrgID = sessionCurrent.OrgID;
				return View();
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "打开页面出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		private VMS_Sys_User GetSearchModelFromJson(JObject paras)
		{
			VMS_Sys_User vm = new VMS_Sys_User();
			vm.pnum = TextHelper.GetInt(paras, "pnum");
			vm.psize = TextHelper.GetInt(paras, "psize");
			vm.sortorder = TextHelper.GetString(paras, "sortorder");

			vm.code = TextHelper.GetString(paras, "code");
			vm.areaid = TextHelper.GetIntNullable(paras, "areaid");
			vm.deptid = TextHelper.GetIntNullable(paras, "deptid");
			vm.state = TextHelper.GetIntNullable(paras, "state");
			vm.orgids = TextHelper.GetInts(paras, "orgid");
			vm.orgname = TextHelper.GetString(paras, "orgname");
			vm.gender = TextHelper.GetIntNullable(paras, "gender");
			vm.name = TextHelper.GetString(paras, "name");
			vm.mobile = TextHelper.GetString(paras, "mobile");
			vm.idcardno = TextHelper.GetString(paras, "idcardno");
			vm.birthdate_s = TextHelper.GetDateTimeStart(paras, "birthdate_s");
			vm.birthdate_e = TextHelper.GetDateTimeEnd(paras, "birthdate_e");
			vm.ids = TextHelper.GetInts(paras, "ids");
			return vm;
		}

		private VMS_Sys_User GetSearchModelFromGetRequest()
		{
			VMS_Sys_User vm = new VMS_Sys_User();
			vm.pnum = TextHelper.GetInt(Request.Query["pnum"]);
			vm.psize = TextHelper.GetInt(Request.Query["psize"]);
			vm.sortorder = TextHelper.GetString(Request.Query["sortorder"]);

			vm.code = TextHelper.GetString(Request.Query["code"]);
			vm.areaid = TextHelper.GetIntNullable(Request.Query["areaid"]);
			vm.deptid = TextHelper.GetIntNullable(Request.Query["deptid"]);
			vm.state = TextHelper.GetIntNullable(Request.Query["state"]);
			vm.orgids = TextHelper.GetInts(Request.Query["orgid"]);
			vm.orgname = TextHelper.GetString(Request.Query["orgname"]);
			vm.gender = TextHelper.GetIntNullable(Request.Query["gender"]);
			vm.name = TextHelper.GetString(Request.Query["name"]);
			vm.mobile = TextHelper.GetString(Request.Query["mobile"]);
			vm.idcardno = TextHelper.GetString(Request.Query["idcardno"]);
			vm.birthdate_s = TextHelper.GetDateTimeStart(Request.Query["birthdate_s"]);
			vm.birthdate_e = TextHelper.GetDateTimeEnd(Request.Query["birthdate_e"]);
			vm.ids = TextHelper.GetInts(Request.Query["ids"]);
			return vm;
		}

		private VMS_Sys_User GetSearchModelFromPostRequest()
		{
			VMS_Sys_User vm = new VMS_Sys_User();
			vm.pnum = TextHelper.GetInt(Request.Form["pnum"]);
			vm.psize = TextHelper.GetInt(Request.Form["psize"]);
			vm.sortorder = TextHelper.GetString(Request.Form["sortorder"]);

			vm.code = TextHelper.GetString(Request.Form["code"]);
			vm.areaid = TextHelper.GetIntNullable(Request.Form["areaid"]);
			vm.deptid = TextHelper.GetIntNullable(Request.Form["deptid"]);
			vm.state = TextHelper.GetIntNullable(Request.Form["state"]);
			vm.orgids = TextHelper.GetInts(Request.Form["orgid[]"]);
			vm.orgname = TextHelper.GetString(Request.Form["orgname"]);
			vm.gender = TextHelper.GetIntNullable(Request.Form["gender"]);
			vm.name = TextHelper.GetString(Request.Form["name"]);
			vm.mobile = TextHelper.GetString(Request.Form["mobile"]);
			vm.idcardno = TextHelper.GetString(Request.Form["idcardno"]);
			vm.birthdate_s = TextHelper.GetDateTimeStart(Request.Form["birthdate_s"]);
			vm.birthdate_e = TextHelper.GetDateTimeEnd(Request.Form["birthdate_e"]);
			vm.ids = TextHelper.GetInts(Request.Form["ids[]"]);
			return vm;
		}

		[HttpPost("search")]
		public IActionResult Search([FromBody] JObject paras)
		{
			try
			{
				UserSession sessionCurrent = GetSessionFromRequest();
				VMS_Sys_User searchModel = GetSearchModelFromJson(paras);
				if (sessionCurrent.OrgID > 0)
				{
					searchModel.orgids = new int[] { sessionCurrent.OrgID };
				}
				PageData pageData = service.GetDetailPageList(searchModel);

				return new JsonResultPage(pageData);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "请求出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		[ValidateSession]
		[HttpGet("add")]
		public IActionResult Add()
		{
			ViewBag.ServiceCode = "1DE4B6114D";
			ViewBag.RelatedModules = relatedModules;
			ViewBag.GenderOptions = GetEnums(typeof(Gender));
			ViewBag.RoleOptions = serviceSysRole.GetRoleSelectListItems();
			ViewBag.UserTypeOptions = GetEnums(typeof(UserType));

			Sys_User vm = new Sys_User()
			{
				Gender = (int)Gender.未知
			};
			return View("Edit", vm);
		}

		[ValidateSession]
		[HttpGet("edit/{id}")]
		[HttpGet("edit")]
		public IActionResult Edit(string id)
		{
			ViewBag.ServiceCode = "1DE4B6114D";
			ViewBag.RelatedModules = relatedModules;
			ViewBag.GenderOptions = GetEnums(typeof(Gender));
			ViewBag.UserTypeOptions = GetEnums(typeof(UserType));
			try
			{
				UserSession sessionCurrent = GetSessionFromRequest();
				int rowID = TextHelper.GetInt(id);
				var vm = service.GetEditDetail(rowID, sessionCurrent);

				ViewBag.RoleOptions = serviceSysRole.GetRoleSelectListItems(vm.RolesID);

				return View(vm);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex, "打开编辑页面出错");
				return new JsonResultBase(false, "打开编辑页面失败，" + (ex.InnerException != null ? ex.InnerException.Message : ex.Message), (int)HttpStatusCode.InternalServerError);
			}
		}

		[ValidateSession]
		[HttpPost("save")]
		public IActionResult Save([FromBody] Sys_User model)
		{
			try
			{
				CheckParams();

				UserSession sessionCurrent = GetSessionFromRequest();

				int rowID = service.AddOrUpdate(model, sessionCurrent);

				return new JsonResultBase(rowID);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "保存失败", (int)HttpStatusCode.InternalServerError);
			}
		}

		/// <summary>
		/// 获取指定用户个人信息
		/// </summary>
		/// <returns></returns>
		[ValidateSession]
		[HttpGet("view/{id}")]
		[HttpGet("view")]
		public IActionResult ViewDetail(string id)
		{
			ViewBag.ServiceCode = "1DE4B6114D";
			ViewBag.RelatedModules = relatedModules;
			try
			{
				int rowID = TextHelper.GetInt(id);

				UserSession sessionCurrent = GetSessionFromRequest();

				var vm = service.GetViewDetail(rowID, sessionCurrent);

				return View("View", vm);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "请求出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		/// <summary>
		/// 获取指定用户个人信息
		/// </summary>
		/// <returns></returns>
		[ValidateSession]
		[HttpPost("detail")]
		public IActionResult ViewJson([FromBody] JObject paras)
		{
			try
			{
				CheckParams();

				int id = TextHelper.GetInt(paras, "id");

				var model = service.GetDetail(id);

				if (model == null)
				{
					return new JsonResultBase(false, "未找到相关记录", (int)HttpStatusCode.NotFound);
				}

				return new JsonResultBase(model);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "请求出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		[ValidateSession]
		[HttpPost("delete")]
		public IActionResult Delete([FromBody] JObject paras)
		{
			try
			{
				CheckParams();

				UserSession sessionCurrent = GetSessionFromRequest();
				var rowID = TextHelper.GetInt(paras, "id");
				var result = service.Delete(rowID, sessionCurrent);

				return new JsonResultBase(result);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "删除失败", (int)HttpStatusCode.InternalServerError);
			}
		}

		[ValidateSession]
		[HttpPost("batchdelete")]
		public IActionResult BatchDelete([FromBody] int[] ids)
		{
			try
			{
				CheckParams();

				UserSession sessionCurrent = GetSessionFromRequest();

				var result = service.BatchDelete(ids, sessionCurrent);

				return new JsonResultBase(result);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "删除失败", (int)HttpStatusCode.InternalServerError);
			}
		}

		/// <summary>
		/// 获取当前用户个人信息
		/// </summary>
		/// <returns></returns>
		[HttpPost("my")]
		public IActionResult ViewMyDetail()
		{
			try
			{
				UserSession sessionCurrent = GetSessionFromRequest();

				SysUserDetail info = service.GetDetail(sessionCurrent.UserID);

				if (info == null)
				{
					throw new ArgumentException("未找到相关记录");
				}

				return new JsonResultBase(info);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "请求出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		/// <summary>
		/// 网页端登录
		/// </summary>
		/// <returns></returns>
		[HttpGet("login")]
		public IActionResult Login()
		{
			return View();
		}

		/// <summary>
		/// 手机端登录
		/// </summary>
		/// <returns></returns>
		[HttpGet("mlogin")]
		public IActionResult MobileLogin()
		{
			return View();
		}

		/// <summary>
		/// 网页端登录
		/// </summary>
		/// <returns></returns>
		[HttpPost("login")]
		public IActionResult Login([FromBody] VM_Login info)
		{
			try
			{
				CheckParams();

				if (info == null)
				{
					throw new ArgumentException("请求参数不能为空");
				}

				UserSession sessionCurrent = service.Login(info.ucode, info.upasswd);
				//获取单位绑定的PartnerID
				if (sessionCurrent.OrgID > 0 && sessionCurrent.OrgPartnerID == 0)
				{
					var org = serviceSysOrganization.Get(sessionCurrent.OrgID);
					if (org != null)
					{
						sessionCurrent.OrgType = org.Type;
						sessionCurrent.OrgPartnerID = org.PartnerID;
					}
				}
				string authSessionBase64Str = sessionCurrent.WriteToResponseCookie(Request, Response);
				JsonResultBase respRet = new JsonResultBase(new
				{
					skey = authSessionBase64Str,
					ucode = sessionCurrent.UserCode,
					name = sessionCurrent.UserName,
					avatar = sessionCurrent.Avatar,
					lastdate = sessionCurrent.LastLoginDate,
					lastip = sessionCurrent.LastLoginIP,
					//dept = sessionCurrent.DeptName
				});

				return respRet;
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "登录出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		/// <summary>
		/// 修改密码
		/// </summary>
		/// <returns></returns>
		[HttpGet("changepasswd")]
		public IActionResult ChangePasswd()
		{
			return View();
		}

		[HttpGet("mchangepasswd")]
		public IActionResult MobileChangePasswd()
		{
			return View();
		}

		/// <summary>
		/// 修改密码
		/// </summary>
		/// <returns></returns>
		[HttpPost("changepasswd")]
		public IActionResult ChangePasswd([FromBody] VM_ChangePasswd info)
		{
			try
			{
				CheckParams();

				if (info == null)
				{
					throw new ArgumentException("请求参数不能为空");
				}

				UserSession sessionCurrent = GetSessionFromRequest();

				bool success = service.ChangePasswd(info.oldpasswd, info.newpasswd, sessionCurrent);

				return new JsonResultBase(success);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "修改密码出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		/// <summary>
		/// 重置密码
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		[HttpPost("resetpasswd")]
		public IActionResult ResetPasswd([FromBody] JObject obj)
		{
			try
			{
				CheckParams();

				UserSession sessionCurrent = GetSessionFromRequest();

				int id = TextHelper.GetInt(obj, "id");

				bool success = service.ResetPasswd(id, sessionCurrent);

				return new JsonResultBase(success);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "重置密码出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		/// <summary>
		/// 网页端退出
		/// </summary>
		/// <returns></returns>
		[HttpGet("logout")]
		public IActionResult Logout()
		{
			Response.Cookies.Delete("skey");
			Response.Cookies.Delete("ucode");
			return View("Login");
		}

		/// <summary>
		/// 网页端退出
		/// </summary>
		/// <returns></returns>
		[HttpGet("mlogout")]
		public IActionResult MobileLogout()
		{
			Response.Cookies.Delete("skey");
			Response.Cookies.Delete("ucode");
			return View("MobileLogin");
		}

		[HttpPost("audit")]
		public IActionResult Audit([FromBody] JObject obj)
		{
			try
			{
				CheckParams();

				UserSession sessionCurrent = GetSessionFromRequest();

				int id = TextHelper.GetInt(obj, "id");
				int state = TextHelper.GetInt(obj, "state");
				string remark = obj.Value<string>("remark");

				bool success = service.Audit(id, state, remark, sessionCurrent);

				return new JsonResultBase(success, success ? "成功" : "审核失败");
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "请求出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		[HttpPost("export")]
		public IActionResult Export()
		{
			try
			{
				//CheckParams();
				var searchModel = GetSearchModelFromPostRequest();
				//if ((searchModel.ids == null || searchModel.ids.Length == 0) && string.IsNullOrEmpty(searchModel.name) && string.IsNullOrEmpty(searchModel.idCardNo))
				//{
				//	if (!searchModel.orgID.HasValue || searchModel.orgID.Value <= 0)
				//	{
				//		throw new ArgumentException("请选择学校");
				//	}
				//	if (!searchModel.grade.HasValue || searchModel.grade.Value <= 0)
				//	{
				//		throw new ArgumentException("请选择年级");
				//	}
				//	if (string.IsNullOrEmpty(searchModel.className))
				//	{
				//		throw new ArgumentException("请选择班级");
				//	}
				//}
				var result = service.GetList(searchModel);
				string fileName = "用户导出-" + DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
				byte[] bytes = service.GenerateExcel(result);
				return File(bytes, "application/vnd.ms-excel", fileName);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "导出失败，" + ex.Message);
			}
		}

		[ValidateSession]
		[HttpPost("validatecode")]
		public IActionResult ValidateCode([FromBody] JObject paras)
		{
			try
			{
				CheckParams();

				UserSession sessionCurrent = GetSessionFromRequest();
				var rowID = TextHelper.GetInt(paras, "id");
				string code = TextHelper.GetString(paras, "code");
				var result = service.ValidateCode(rowID, code, sessionCurrent);

				return new JsonResultBase(result, result ? "登录账号可用" : "登录账号不可用");
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "删除失败", (int)HttpStatusCode.InternalServerError);
			}
		}

		#region 接口相关
		[HttpPost("loginm")]
		public IActionResult LoginByMobile([FromForm] VM_Login info)
		{
			try
			{
				Response.Headers.Add("Access-Control-Allow-Origin", "*");
				//Response.Headers.Append("Access-Control-Allow-Methods", "*");
				//Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE,ucode,skey");

				CheckParams();
				UserSession sessionCurrent = service.Login(info.ucode, info.upasswd);
				string authSessionBase64Str = sessionCurrent.WriteToResponseCookie(Request, Response);//todo:跨站写cookie没有用？
				JsonResultBase respRet = new JsonResultBase(new
				{
					skey = authSessionBase64Str,
					ucode = sessionCurrent.UserCode,
					name = sessionCurrent.UserName,
					avatar = sessionCurrent.Avatar,
					lastdate = sessionCurrent.LastLoginDate,
					lastip = sessionCurrent.LastLoginIP,
					utype = sessionCurrent.RoleType,
					//org = sessionCurrent.OrgName,
					//dept = sessionCurrent.DeptName
				});

				return respRet;
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "请求出错，" + ex.Message);
			}
		}
		#endregion
	}
}